import { request } from "../../request/index.js"
import regeneratorRuntime from '../../lib/runtime/runtime'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    leftMenuList: [],
    rightContent: [],
    currentIndex: 0,
    scrooltop:0
  },
  CatesList: [],
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getCatesList();
    // 先查看本地是否有数据
    const Cates = wx.getStorageSync("cates");
    //如果没有
    if (!Cates) {
      this.getCatesList();
    } else {
      if (Date.now() - Cates.time > 1000 * 10) {
        // 超过时间重新发送请求
        this.getCatesList();
      } else {
        // 未超过时间
        this.CatesList = Cates.data;
        let leftMenuList = this.CatesList.map(v => v.cat_name)
        let rightContent = this.CatesList[0].children;
        this.setData({
          leftMenuList,
          rightContent
        })
      }

    }


  },

  // 获取分类数据
 async getCatesList() {
    // request({ url: '/categories' })
    //   .then(result => {
    //     // 得到请求的数据
    //     this.CatesList = result.data.message;
    //     //先将数据封装进本地
    //     wx.setStorageSync("cates", { time: Date.now(), data: this.CatesList });
    //     let leftMenuList = this.CatesList.map(v => v.cat_name)
    //     let rightContent = this.CatesList[0].children;
    //     this.setData({
    //       leftMenuList,
    //       rightContent
    //     })
    //   })
   const result =await request({ url: '/categories' })
   this.CatesList = result.data.message;
        //先将数据封装进本地
        wx.setStorageSync("cates", { time: Date.now(), data: this.CatesList });
         let leftMenuList = this.CatesList.map(v => v.cat_name)
         let rightContent = this.CatesList[0].children;
        this.setData({
          leftMenuList,
           rightContent
       })
       

  },
  // 左侧菜单点击事件
  handleitemtap(e) {
    const index = e.currentTarget.dataset.index;

    let rightContent = this.CatesList[index].children;
    this.setData({
      currentIndex: index,
      rightContent,
      scrooltop:0
    })
  
    
  }
})